package simulateOS.UI;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import simulateOS.entity.FAT;

import java.io.File;

import static javafx.stage.StageStyle.TRANSPARENT;

public class FileUI {
    private BorderPane titleBlock = new BorderPane();
    private BorderPane mainUI = new BorderPane();
    private Button save = new Button("保存");
    private Stage stage = new Stage();
    //设置程序的名字和程序图片
//    private File imagePath = new File("src\\main\\resources\\image");
//    private String imagePath = "src/main/resources/image";
    private ClassLoader classLoader = FileUI.class.getClassLoader();
    private Image stageImage = new Image(classLoader.getResourceAsStream("image/stage.png"));
    private Image stageName = new Image(classLoader.getResourceAsStream("image/stageName.png"));

    public FileUI(simulateOS.entity.File file, int type) {
        if(file.getFlag() == 0){
            stageName = new Image(classLoader.getResourceAsStream("image/stageNameR.png"));
        }
        //可执行文件设置
        if(file.isExecutableFile()){
            stageImage = new Image(classLoader.getResourceAsStream("image/stageE.png"));
            stageName = new Image(classLoader.getResourceAsStream("image/stageNameE.png"));
            if(file.getFlag() == 0){
                stageName = new Image(classLoader.getResourceAsStream("image/stageNameER.png"));
            }
        }
        //去掉系统自带的标题栏
        stage.initStyle(TRANSPARENT);
        //设置标题栏
        HBox title = new HBox();
        title.getChildren().addAll(new ImageView(stageImage), new ImageView(stageName));
        //重新构建全屏缩小关闭
        Image image = new Image(classLoader.getResourceAsStream("image/-.png"));
        Button minButton = new Button();
        minButton.setGraphic(new ImageView(image));
        image = new Image(classLoader.getResourceAsStream("image/O.png"));
        Button maxButton = new Button("");
        maxButton.setGraphic(new ImageView(image));
        image = new Image(classLoader.getResourceAsStream("image/X.png"));
        Button closeButton = new Button("");
        closeButton.setGraphic(new ImageView(image));
        minButton.setStyle(" -fx-border-width: 0.1;-fx-text-fill: white ; -fx-border-image-insets: 0;-fx-background-insets: 0;");
        maxButton.setStyle(" -fx-border-width: 0.1;-fx-text-fill: white ; -fx-border-image-insets: 0;-fx-background-insets: 0;");
        closeButton.setStyle(" -fx-border-width: 0.1;-fx-text-fill: red ; -fx-border-image-insets: 0;-fx-background-insets: 0;");
        minButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.setIconified(true);
            }
        });
        maxButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //stage.setMaximized(!stage.isMaximized());
            }
        });
        closeButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.close();
                file.setOpen(false);
            }
        });
        HBox hBox = new HBox();
        hBox.getChildren().addAll(save,minButton, maxButton, closeButton);
        titleBlock.setStyle("-fx-background-color:white");
        titleBlock.setLeft(title);
        titleBlock.setRight(hBox);
        DragWindowHandler dragWindowHandler = new DragWindowHandler(stage);
        titleBlock.setOnMousePressed(dragWindowHandler);/* 鼠标按下 */
        titleBlock.setOnMouseDragged(dragWindowHandler);/* 鼠标拖动 */
        TextArea textArea = new TextArea(file.getContent());
        if(type == 0){
            textArea.setEditable(false);
        }else if(type == 1){
            textArea.setEditable(true);
        }
        save.setPrefHeight(30);
        FlowPane flowPane = new FlowPane(textArea);
        textArea.setPrefHeight(350);
        mainUI.setTop(titleBlock);
        ScrollPane scrollPane = new ScrollPane(flowPane);
        mainUI.setCenter(scrollPane);
        Scene scene = new Scene(mainUI,400,300);
        stage.setScene(scene);

        save.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                if(type == 0){
                    save.setStyle("-fx-text-fill:gray");
                }else if(type == 1){
                    save.setStyle("-fx-text-fill:black");
                    FAT.getFAT().showAlert(FAT.getFAT().saveFile(file,textArea.getText()));
                    DiskUI.getDiskUI().initTheGridPane();
                    FileAppUI.getFileAppUI().openFolder(file.getParent().getPath().getPathName());
                }
            }
        });
    }

    public Stage getStage() {
        return stage;
    }
}
